<%@ include file="/WEB-INF/common/taglibs.jsp"%>
<%@ page import="java.util.List"%>
<%@ page import="com.oss.model.*"%>
<%@ page import="com.oss.service.system.CacheService"%>
<%
	// 生成Option选项
	List<Tag> tags = CacheService.getChildTags();
	StringBuilder sb = new StringBuilder();
	sb.append("<select>");
	sb.append("<option value='notSet'>notSet</option>");
	for (Tag tag : tags) {
		sb.append("<option value='").append(tag.getId()).append("'>")
				.append(tag.getName()).append("</option>");
	}
	sb.append("</select>");
	String selectTag = sb.toString();
	
	List<Project> prjs = CacheService.getPrjs();
	sb = new StringBuilder();
	sb.append("<select>");
	sb.append("<option value='notSet'>notSet</option>");
	for(Project prj:prjs){
		sb.append("<option value='").append(prj.getId()).append("'>")
		.append(prj.getName()).append("</option>");
	}
	sb.append("</select>");
	String selectPrj = sb.toString();
	
	List<Topic> topics = CacheService.getTopics();
	sb = new StringBuilder();
	sb.append("<select>");
	sb.append("<option value='notSet'>notSet</option>");
	for(Topic t: topics){
		sb.append("<option value='").append(t.getId()).append("'>")
		.append(t.getName()).append("</option>");
	}
	sb.append("</select>");
	String selectTopic = sb.toString();
%>
<script type="text/javascript"> 
var lnk='';
$(document).ready(function(){	
	$("#table_lnks").tableSorter({
		sortClassAsc: 'headerSortUp',
		sortClassDesc: 'headerSortDown',
		headerClass: 'header'
	}).tableEditor({
		EDIT_HTML: 'Edit',
		SAVE_HTML: 'Save',
		FUNC_PRE_EDIT: 'preEdit',
		FUNC_PRE_SAVE: 'preSave',
		FUNC_UPDATE: 'updateTable'
	});
});
function preEdit(o){
	var col = o.row[3];
	var val = col.innerHTML;
	var html = "<%=selectTag%>";
	$(col).html(html).find('select option').each(function(){
		if ($(this).val() == val){
			$(this).attr("selected", true);
		}
	});

	col = o.row[4];
	val = col.innerHTML;
	html = "<%=selectTopic%>";
	$(col).html(html).find('select option').each(function(){
		if ($(this).val() == val){
			$(this).attr("selected", true);
		}
	});

	col = o.row[5];
	val = col.innerHTML;
	html = "<%=selectPrj%>";
	$(col).html(html).find('select option').each(function(){
		if ($(this).val() == val){
			$(this).attr("selected", true);
		}
	});
}
function preSave(o){
	lnk='';
	o.row.find("input,select").each(function(){
		lnk += $(this).val()+"-,-";
	}).end();
}
function updateTable(o){
	$.ajax({
		url: '/ajax/admin/updateLink.do?lnk=' + lnk,
		type: 'POST',
		timeout: 10000,
		error: function(){alert('update failed!');},
		success: function(){}
	});
	var val = $(o.row[3]).find("select").val();
	$(o.row[3]).html(val);
	val = $(o.row[4]).find("select").val();
	$(o.row[4]).html(val);
	val = $(o.row[5]).find("select").val();
	$(o.row[5]).html(val);
}
function deleteLink(id){
	$.ajax({
		url: '/ajax/admin/deleteLink.do?id=' + id,
		type: 'POST',
		timeout: 10000,
		error: function(){alert('delete failed!');},
		success: function(){}
	});
}
</script>

<div class="pageMenu"><c:if test="${param.pageIndex gt 1}">
	<a
		href="<c:url value="/admin/batchEditLnks.do?prj=${param.prj}&pageIndex=${param.pageIndex-1}&pageSize=${param.pageSize}"/>">Previous&nbsp;Page</a>
</c:if>&nbsp;&nbsp;<a
	href="<c:url value="/admin/batchEditLnks.do?prj=${param.prj}&pageIndex=${param.pageIndex+1}&pageSize=${param.pageSize}"/>">Next&nbsp;Page</a></div>
<table align="center" id="table_lnks">
	<thead>
		<tr>
			<th>Id</th>
			<th style="width: 150px;">Name</th>
			<th>Source</th>
			<th style="width: 180px;">Tag</th>
			<th style="width: 120px;">Topic</th>
			<th style="width: 120px;">Project</th>
			<th>Enabled</th>
			<th>Action</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach var="lnk" items="${lnks}">
			<tr>
				<td>${lnk.id}</td>
				<td>${lnk.name}</td>
				<td>${lnk.source}</td>
				<td>${lnk.tag}</td>
				<td>${lnk.topic}</td>
				<td>${lnk.prj}</td>
				<td>${lnk.enabled}</td>
				<td><a href="#" class="tsEditLink">Edit</a>&nbsp;<a href="#"
					onclick="deleteLink(${lnk.id})">Delete</a></td>
			</tr>
		</c:forEach>
	</tbody>
</table>
<div class="pageMenu"><c:if test="${param.pageIndex gt 1}">
	<a
		href="<c:url value="/admin/batchEditLnks.do?prj=${param.prj}&pageIndex=${param.pageIndex-1}&pageSize=${param.pageSize}"/>">Previous&nbsp;Page</a>
</c:if>&nbsp;&nbsp;<a
	href="<c:url value="/admin/batchEditLnks.do?prj=${param.prj}&pageIndex=${param.pageIndex+1}&pageSize=${param.pageSize}"/>">Next&nbsp;Page</a></div>